//
//  ZCuserAccountViewModel.swift
//  weibo
//
//  Created by 张城 on 16/7/22.
//  Copyright © 2016年 张城. All rights reserved.
//

import UIKit

class ZCuserAccountViewModel: NSObject {
    
    let filePath = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("userAccount.data")
    
    static let sharedTools: ZCuserAccountViewModel = ZCuserAccountViewModel()
    
    var userAccount: ZCUserAccount?
    
    override init() {
        super.init()
        
        userAccount = getUserAccount()
    }
    
    
    var accessToken: String? {
        
        if userAccount?.access_token != nil {
            
            if userAccount?.expires_date?.compare(NSDate()) == .OrderedDescending {
                return userAccount?.access_token
            }else{
                return nil
            }
        }else{
            return nil
        }
    }
    
    var isLogin: Bool {
        return (accessToken != nil)
    }
    
}


extension ZCuserAccountViewModel {
    
    func saveUserAccount(userAccount: ZCUserAccount) {
   
        self.userAccount = userAccount
        
        NSKeyedArchiver.archiveRootObject(userAccount, toFile: filePath)
    }
    
    func getUserAccount()-> ZCUserAccount? {
        
        let result = NSKeyedUnarchiver.unarchiveObjectWithFile(filePath) as? ZCUserAccount
        
        return result
    }
    
    
    
}


// MARK:-- 请求数据
extension ZCuserAccountViewModel {
    
    func loadUserAccount(code: String, isSucess:(isSuc: Bool)->()) {
        
        ZCNetworkTools.sharedTool.oauthLoadUserAccount(code, success: { (response) in
            
            guard let res = response as? [String : AnyObject] else{
                isSucess(isSuc: false)
                return
            }
            let result = ZCUserAccount(dic:res)
            
            self.loadUserInfo(result, isSucess: isSucess)
            //            print(result)
//            isSucess(isSuc: true)
        }) { (error) in
            isSucess(isSuc: false)
        }
    }
    
    func loadUserInfo(userAccount: ZCUserAccount,isSucess:(isSuc: Bool)->()) {
        
        ZCNetworkTools.sharedTool.oauthLoadUserInfo(userAccount, success: { (response) in
            
            guard let res = response as? [String : AnyObject] else{
                isSucess(isSuc: false)
                return
            }
            //            let result = ZCUserAccount(dic:res)
            userAccount.screen_name = res["screen_name"] as? String
            userAccount.avatar_large = res["avatar_large"] as? String
            
            
            
            
            //            print(userAccount.screen_name ?? "",userAccount.avatar_large ?? "")
            self.saveUserAccount(userAccount)
//                        print(userAccount.avatar_large)
            
            
            
            isSucess(isSuc: true)
            
        }) { (error) in
            isSucess(isSuc: false)
        }
        
    }
    
}






















